package com.h.heal.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h.heal.common.MyPageInfo;
import com.h.heal.common.SessionParameter;
import com.h.heal.entity.User;
import com.h.heal.mapper.UserMapper;
import com.h.heal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(User user, HttpSession httpSession) {
        User userNameDB = userMapper.findByName(user.getUser_name());
//        User userPwdDB = userMapper.findByPwd(user.getUser_pwd());
        User userDB = userMapper.findByNameAndPwd(user.getUser_name(), user.getUser_pwd());
        if (userNameDB == null) {
            return "error_name";
//        } else if(userPwdDB == null){    //验证密码怎么验证
//            return "error_pwd";
        } else if (userDB != null) {
            httpSession.setAttribute(SessionParameter.USER_INFO, userDB);
            return "ok";
        } else {
            return "error_pwd";//本来是error_unknow
        }
    }

    @Override
    public String register(User user) {
        User userDB = userMapper.findByName(user.getUser_name());
        if (userDB != null) {
            return "error";
        } else {
            userMapper.register(user);
            return "ok";
        }
    }


    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findById(int id) {
        return userMapper.findById(id);
    }

    @Override
    public List<User> findByUser_name(String user_name) {
        return userMapper.findByUser_name(user_name);
    }

    @Override
    public List<User> findByUser_pwd(String user_pwd) {
        return userMapper.findByUser_pwd(user_pwd);
    }

    @Override
    public List<User> findByUser_vip(String user_vip) {
        return userMapper.findByUser_vip(user_vip);
    }

    @Override
    public List<User> findByUser_state(String user_state) {
        return userMapper.findByUser_state(user_state);
    }

    @Override
    public void deleteByUser_uid(int id) {
        userMapper.deleteByUser_uid(id);
    }

    @Override
    public String edit(User user) {
        try {
            userMapper.edit(user);
            return "ok";
        } catch (Exception e) {
            System.out.println("UserServiceImpl.edit() ERROR:e=" + e);

        }
        return "error";
    }

    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String user_name = request.getParameter("user_name");
        String user_pwd = request.getParameter("user_pwd");
        String user_vip = request.getParameter("user_vip");
        String user_state = request.getParameter("user_state");


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        userMapper.findBySearch(user_name, user_pwd, user_vip, user_state);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());


        return myPageInfo;
    }
}
